package com.fourth_groups.shopping121store.ui.home.view;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.fourth_groups.shopping121store.R;
import com.fourth_groups.shopping121store.ui.home.model.HomeFoodBean;

import java.util.List;

/**
 * ----------Dragon be here!----------/
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃  创建人:李培雄
 * 　　┃　┳┛　┗┳　┃  时间:2016/10/13 08:46
 * 　　┃　　　　　　　┃  备注:
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃代码无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━by:coder-pig
 */
class HomeRecylerViewAdapter extends RecyclerView.Adapter<HomeRecylerViewAdapter.MyViewHolder> implements DialogInterface.OnClickListener {
    private Context context;
    private static List<HomeFoodBean.DataBean.BlockBean> mData;

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int position, List<HomeFoodBean.DataBean.BlockBean> mData);
    }

    private static OnRecyclerViewItemClickListener mListener;

    //设置条目的点击事件
    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener listener) {
        mListener = listener;
    }

    HomeRecylerViewAdapter() {

    }

    public void setData(List<HomeFoodBean.DataBean.BlockBean> mDatas) {
        mData = mDatas;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_fragment_recyler, parent, false);
        this.context = parent.getContext();
        return new MyViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        Log.w("===", "----------------------------onBindViewHolder: " + mData.get(position).getItems().get(0).getTitle());
        if (mData.get(position).getItems().get(0).getConfig().getMarket_price() != null) {
            holder.title.setText(mData.get(position).getItems().get(0).getTitle());
            holder.price.setText("￥" + mData.get(position).getItems().get(0).getConfig().getPrice());
            holder.market.setText(mData.get(position).getItems().get(0).getConfig().getMarket_price());
            holder.rebate.setText(mData.get(position).getItems().get(0).getConfig().getRebate());
            holder.xian.setVisibility(View.VISIBLE);
            holder.xian.setText(mData.get(position).getItems().get(0).getConfig().getMarket_price());
            ImageView rm = holder.rm;
            Glide.with(context).load(mData.get(position).getItems().get(0).getCorner_ico()).into(rm);
        } else {
            holder.zhuan.setVisibility(View.GONE);
            holder.panic.setVisibility(View.GONE);
            holder.price.setVisibility(View.GONE);
            holder.title.setVisibility(View.GONE);
            holder.price.setVisibility(View.GONE);
            holder.market.setVisibility(View.GONE);
            holder.rebate.setVisibility(View.GONE);
            holder.xian.setVisibility(View.GONE);
            holder.rm.setVisibility(View.GONE);
        }
        ImageView image = holder.image;
        Glide.with(context).load(mData.get(position).getItems().get(0).getImage()).error(R.drawable.hongbao_logo).into(image);
    }

    @Override
    public int getItemCount() {
        return mData != null ? mData.size() : 0;
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {

    }

    static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private final ImageView image;
        private final TextView title;
        private final TextView rebate;
        private final TextView price;
        private final TextView market;
        private final ImageView rm;
        private final TextView xian;
        private final Button panic;
        private final TextView zhuan;

        MyViewHolder(final View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.home_fragment_title);
            xian = (TextView) itemView.findViewById(R.id.home_xian);
            rebate = (TextView) itemView.findViewById(R.id.home_fragment_rebate);
            price = (TextView) itemView.findViewById(R.id.home_fragment_price);
            market = (TextView) itemView.findViewById(R.id.home_fragment_market_price);
            rm = (ImageView) itemView.findViewById(R.id.homes_recycler_rm);
            image = (ImageView) itemView.findViewById(R.id.homes_recycler_image);
            panic = (Button) itemView.findViewById(R.id.home_fragment_panic);
            zhuan = (TextView) itemView.findViewById(R.id.home_zhuan);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (v != null) {
                mListener.onItemClick(v, getLayoutPosition(), mData);
            }
        }
    }
}
